package com.sanjin.service.impl;

import com.sanjin.entity.ParkOrder;
import com.sanjin.entity.ParkSpace2WxMiniUser;
import com.sanjin.exception.MyException;
import com.sanjin.repository.ParkSpace2WxMiniUserRepository;
import com.sanjin.service.ParkOrderService;
import com.sanjin.service.ParkSpace2WxMiniUserService;
import com.sanjin.service.WxMiniUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/5/15 21:29
 */
@Service
public class ParkSpace2WxMiniUserServiceImpl implements ParkSpace2WxMiniUserService {

    @Autowired
    private ParkSpace2WxMiniUserRepository repository;

    @Autowired
    private ParkOrderService orderService;

    @Autowired
    private WxMiniUserService wxMiniUserService;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public ParkSpace2WxMiniUser createCarEnter(String carLicenseNumber) {
        ParkSpace2WxMiniUser parkSpace2WxMiniUser =
                new ParkSpace2WxMiniUser()
                        .setWxMiniUserId(wxMiniUserService.queryWxMiniUserByCarLicenseNumber(carLicenseNumber).getId())
                        // 进入停车场 direction 为 0
                        // 类型(0 -> 进入停车场，1 -> 离开停车场)
                        .setDirection(0);
        return repository.save(parkSpace2WxMiniUser);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public ParkOrder createCarLeave(String carLicenseNumber) {
        ParkSpace2WxMiniUser parkSpace2WxMiniUser =
                new ParkSpace2WxMiniUser()
                        .setWxMiniUserId(wxMiniUserService.queryWxMiniUserByCarLicenseNumber(carLicenseNumber).getId())
                        // 进入停车场 direction 为 0
                        // 类型(0 -> 进入停车场，1 -> 离开停车场)
                        .setDirection(1);
        // 保存离开停车场记录
        repository.saveAndFlush(parkSpace2WxMiniUser);

        // 查询该所有车牌号进入停车场记录
        List<ParkSpace2WxMiniUser> enterRecords =
                repository.queryParkSpace2WxMiniUsersByWxMiniUserIdAndAndDirection(parkSpace2WxMiniUser.getWxMiniUserId(), 0);
        // 查询该所有车牌号进入停车场记录
        List<ParkSpace2WxMiniUser> leaveRecord =
                repository.queryParkSpace2WxMiniUsersByWxMiniUserIdAndAndDirection(parkSpace2WxMiniUser.getWxMiniUserId(), 1);


        if (enterRecords.size() <= 0) {
            throw new MyException("该车辆没有进入停车场记录");
        }
        // 将 parkSpace2WxMiniUsers 按照 id 排序，找出最后一条进入停车场的记录
        ParkSpace2WxMiniUser enter = null, leave = null;
        List<ParkSpace2WxMiniUser> enterSorted = enterRecords.stream().sorted((p1, p2) -> {
            return p1.getId().compareTo(p2.getId());
        }).collect(Collectors.toList());
        List<ParkSpace2WxMiniUser> leaveSorted = leaveRecord.stream().sorted((p1, p2) -> {
            return p1.getId().compareTo(p2.getId());
        }).collect(Collectors.toList());

        // TODO 进入离开数据量是否合法


        // 最后一条记录
        enter = enterSorted.get(enterSorted.size() - 1);
        leave = leaveSorted.get(leaveRecord.size() - 1);

        // 计算时间
        double duration = System.currentTimeMillis() - enter.getCreateTime().getTime();

        // 生成订单 默认一个小时 1 块钱
        BigDecimal price = new BigDecimal(1);
        BigDecimal spendTime = new BigDecimal(duration / 1000 / 60 / 60);
        ParkOrder parkOrder = new ParkOrder()
                .setAmount(price.multiply(spendTime))
                .setSpendTime(spendTime)
                .setWxMiniUserId(parkSpace2WxMiniUser.getWxMiniUserId());
        // 创建订单
        ParkOrder order = orderService.createOrder(parkOrder);

        return order;
    }
}
